<?php
/*Created by Timothy Cheung*/
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class availability extends CI_Controller {

/*
|--------------------------------------------------------------------------
| availStatus (@author : Timothy Cheung)
|--------------------------------------------------------------------------
| This function returns the parsed availability status as JSON and call the 
| function to wrap the response in the requested callback. 
*/      
    public function availStatus($doc_no){
        //availability_model is set into controller
        $this->load->model('availability_model');
        
        //Load XML returned by X-services API into simplexml format
        $xml = "http://libcat.tp.edu.sg/X?op=circ-status&sys_no=".$doc_no."&library=tpl01";
        $xml_seq = "http://libcat.tp.edu.sg/X?op=item-data&doc_number=".$doc_no."&base=tpl01";
        $load_xml = simplexml_load_file($xml);
        $load_xml_Seq = simplexml_load_file($xml_seq);
        
        //Parse XML into an availability_model record, return as output
        $output = $this -> availability_model -> parseAvailXML($load_xml, $load_xml_Seq, $doc_no);
        $this -> retJsonP($output);
    }
    
/*
|--------------------------------------------------------------------------
| retJsonP (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function wraps the response in the requested callback. 
|
*/    
    function retJsonP($output) {
        $callback = $_REQUEST['callback'];

        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . json_encode($output) . ');';
        } else {
            header('Content-Type: application/x-json');
            echo json_encode($output);
        }
    }

}
?>
